package com.ksd.modules.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.exception.ApiException;
import com.ksd.modules.members.service.IAdminUserService;
import com.ksd.modules.members.vo.AdminUserVo;
import com.ksd.modules.note.entity.AdminNoteUserBuy;
import com.ksd.modules.note.mapper.AdminNoteUserBuyMapper;
import com.ksd.modules.note.po.AdminNoteUserBuySavePO;
import com.ksd.modules.note.po.AdminNoteUserBuySearchPO;
import com.ksd.modules.note.service.IAdminNoteBuyService;
import com.ksd.modules.note.service.IAdminNoteService;
import com.ksd.modules.note.vo.AdminNoteUserBuyVo;
import com.ksd.modules.note.vo.AdminNoteVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminNoteUserBuyServiceImpl extends BaseServiceImpl<AdminNoteUserBuyMapper, AdminNoteUserBuy> implements IAdminNoteBuyService {

    @Autowired
    private IAdminUserService adminUserService;
    @Autowired
    private IAdminNoteService adminNoteService;

    /**
     * 查询列表
     *
     * @param adminNoteUserBuySearchPO
     * @return
     */
    public Page<AdminNoteUserBuyVo> findAdminNoteUserBuys(AdminNoteUserBuySearchPO adminNoteUserBuySearchPO) {
        Page<AdminNoteUserBuy> page = new Page<>(adminNoteUserBuySearchPO.getPageNo(), adminNoteUserBuySearchPO.getPageSize());
        LambdaQueryWrapper<AdminNoteUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteUserBuy::getSystemId, SystemIdThrealLocal.get().getSystemId());
        // 订单编号
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminNoteUserBuySearchPO.getOrderno()), AdminNoteUserBuy::getOrderno, adminNoteUserBuySearchPO.getOrderno());
        // 支付方式
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteUserBuySearchPO.getPayMethod()), AdminNoteUserBuy::getPayMethod, adminNoteUserBuySearchPO.getPayMethod());
        // 具体资源订单
        lambdaQueryWrapper.eq(Objects.nonNull(adminNoteUserBuySearchPO.getOpid()), AdminNoteUserBuy::getOpid, adminNoteUserBuySearchPO.getOpid());
        // 兑换码
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminNoteUserBuySearchPO.getCode()), AdminNoteUserBuy::getCode, adminNoteUserBuySearchPO.getCode());
        // uuid
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminNoteUserBuySearchPO.getUuid()), AdminNoteUserBuy::getUuid, adminNoteUserBuySearchPO.getUuid());
        // 手机
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminNoteUserBuySearchPO.getPhone()), AdminNoteUserBuy::getPhone, adminNoteUserBuySearchPO.getPhone());
        // 模糊匹配
        if (StringUtils.isNotEmpty(adminNoteUserBuySearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(
                            AdminNoteUserBuy::getNickname, adminNoteUserBuySearchPO.getKeyword())
                    .or().like(AdminNoteUserBuy::getUsername, adminNoteUserBuySearchPO.getKeyword())
                    .or().like(AdminNoteUserBuy::getTitle, adminNoteUserBuySearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminNoteUserBuy::getCreateTime);
        Page<AdminNoteUserBuy> adminNotePage = this.page(page, lambdaQueryWrapper);
        Page<AdminNoteUserBuyVo> adminNoteVoPage = tranferPageBo(adminNotePage, AdminNoteUserBuyVo.class);
        return adminNoteVoPage;
    }


    /**
     * 保存和更新
     *
     * @param adminNoteUserBuySavePO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AdminNoteUserBuyVo saveUpdateAdminNoteUserBuy(AdminNoteUserBuySavePO adminNoteUserBuySavePO) {
        AdminNoteUserBuy adminChannel = tranferBo(adminNoteUserBuySavePO, AdminNoteUserBuy.class);
        adminChannel.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminChannel.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminChannel);
        AdminNoteUserBuyVo adminChannelVo = tranferBo(adminChannel, AdminNoteUserBuyVo.class);
        return adminChannelVo;
    }

    /**
     * 根据ID获取明细
     *
     * @param id
     * @return
     */
    public AdminNoteUserBuyVo getAdminNoteUserBuyById(Long id) {
        AdminNoteUserBuy adminChannel = this.getById(id);
        AdminNoteUserBuyVo adminChannelVo = tranferBo(adminChannel, AdminNoteUserBuyVo.class);
        return adminChannelVo;
    }

    /**
     * 单个物理删除
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeAdminNoteUserBuyById(Long id) {
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     *
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchRemovesAdminNoteUserBuy(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, String column, Integer value) {
        boolean b = super.updateStatus(id, column, value);
        return b;
    }

    /**
     * 修改状态
     *
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id, String column, String value) {
        boolean b = super.updateStatusStr(id, column, value);
        return b;
    }


    /**
     * 用户绑定小册订单
     *
     * @param uuid
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean bindUserOrder(String uuid, Long opid) {
        LambdaQueryWrapper<AdminNoteUserBuy> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteUserBuy::getUuid, uuid);
        lambdaQueryWrapper.eq(AdminNoteUserBuy::getOpid, opid);
        AdminNoteUserBuy adminNoteUserBuy = this.getOne(lambdaQueryWrapper);
        if (adminNoteUserBuy != null) {
            throw new ApiException(501, "该用户已绑定此资源!!!");
        }
        adminNoteUserBuy = new AdminNoteUserBuy();
        AdminUserVo adminUserVo = adminUserService.getAdminUserByUUId(uuid);
        AdminNoteVo adminNoteVo = adminNoteService.getAdminNoteById(opid);
        adminNoteUserBuy.setOpid(opid);
        adminNoteUserBuy.setUserId(adminUserVo.getId());
        adminNoteUserBuy.setNickname(adminUserVo.getNickName());
        adminNoteUserBuy.setUsername(adminUserVo.getUserName());
        adminNoteUserBuy.setAvatar(adminUserVo.getAvatar());
        adminNoteUserBuy.setUuid(adminUserVo.getUuid());
        adminNoteUserBuy.setPhone(adminUserVo.getTelephone());
        adminNoteUserBuy.setAddress(adminUserVo.getAddress());

        adminNoteUserBuy.setTitle(adminNoteVo.getTitle());
        adminNoteUserBuy.setDescription(adminNoteVo.getDescription());
        adminNoteUserBuy.setCover(adminNoteVo.getImg());
        adminNoteUserBuy.setCode("");
        adminNoteUserBuy.setPrice(Objects.nonNull(adminNoteVo.getPrice()) ? adminNoteVo.getPrice() : String.valueOf(adminNoteVo.getCoin()));

        adminNoteUserBuy.setOrderno(String.valueOf(new SnowflakeIdWorker(7, 8).nextId()));
        adminNoteUserBuy.setTradeno(String.valueOf(new SnowflakeIdWorker(8, 8).nextId()));
        adminNoteUserBuy.setPayMethod(4);
        adminNoteUserBuy.setPayMethodName("人工");

        adminNoteUserBuy.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminNoteUserBuy.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminNoteUserBuy.setOrderJson("");
        return this.saveOrUpdate(adminNoteUserBuy);
    }
}
